﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ERPStore.Extensions;

namespace ERPStore.Web.Html
{
	public static class ConversionsExtensions
	{
		public static bool IsLeadSourceExists(this ERPStore.Models.UserPrincipal principal, string leadSourceId)
		{
			var visitorService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IVisitorService>();
			bool result = false;
			var visitorHistory = visitorService.GetVisitorHistory(principal.VisitorId);
			if (visitorHistory != null)
			{
				// TODO: Verifier que lors du track , un ajout au panier a bien eu lieu
				// ou que c'est le dernier track qui a permis la vente (retargeting)
				foreach (var track in visitorHistory.TrackList)
				{
					if (track.SourceId != null
						&& track.SourceId.Equals(leadSourceId, StringComparison.InvariantCultureIgnoreCase))
					{
						result = true;
						break;
					}
				}
			}

			return result;
		}

	}
}
